package web;

import domain.User;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findByIdServlet")
public class FindByIdServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //修改用户的信息,点击进入修改页面之后,页面要显示这个用户的信息,也就涉及到数据回显
        //1:获取前端页面的数据
        String id = request.getParameter("id");
        //将数据传递子业务层
        UserServiceImpl service = new UserServiceImpl();

        //调用查询方法
        User user = service.findById(id);

        //将数据响应回去
        if (user != null){
            request.getSession().setAttribute("user",user);
            response.sendRedirect("/update.jsp");
        }
    }
}
